package com.smartcruise.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.smartcruise.MyApplication;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import io.reactivex.annotations.NonNull;

/**
 * Created by FengChaoQun
 * on 2018/2/2 0002
 */

public class SPUtils {

    private static final String FILE_NAME = "smartCruise";
    private static final String LAST_ACCOUNT = "LAST_ACCOUNT";
    private static final String LAST_USER_NAME = "LAST_USER_NAME";
    private static final String LAST_USER_PHONE = "LAST_USER_PHONE";
    private static final String LAST_TOKEN = "LAST_TOKEN";
    public static final String IS_QUIT = "IS_QUIT";

    public static void put(String key, Object object) {

        SharedPreferences sp = MyApplication.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }

        SharedPreferencesCompat.apply(editor);
    }

    public static Object get(String key, Object defaultObject) {
        SharedPreferences sp = MyApplication.getInstance().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);

        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     *
     * @author zhy
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         *
         * @return
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行，否则使用commit
         *
         * @param editor
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
            editor.commit();
        }
    }

    public static void saveAccount(@NonNull String account) {
        put(LAST_ACCOUNT, account);
    }

    public static String getLastAccount() {
        return (String) get(LAST_ACCOUNT, "");
    }

    public static String getLastName() {
        return (String) get(LAST_USER_NAME, "");
    }

    public static void saveName(@NonNull String account) {
        put(LAST_USER_NAME, account);
    }

    public static String getLastPhone() {
        return (String) get(LAST_USER_PHONE, "");
    }

    public static void savePhone(@NonNull String phone) {
        put(LAST_USER_PHONE, phone);
    }

    public static void saveToken(@NonNull String token) {
        put(LAST_TOKEN, token);
    }

    public static String getLastToken() {
        return (String) get(LAST_TOKEN, "");
    }

    public static void setIsQuit(boolean isQuit) {
        put(IS_QUIT, isQuit);
    }

    public static boolean isQuit() {
        return (boolean) get(IS_QUIT, true);
    }

}
